/**
 * Created by geqq on 2018/5/14 0937.
 */
var TransactionIndex = new Vue({
    el: '#transaction',
    data: {
        datas: {
            'paginator':{},
            'billList': []
        },
        orderDetail: {},
        detailShow: false,
        isShowBindAccount: false,
        isShowVerification: false,
        search: {
            pageNo: 1,
            pageSize: 20,
            startDate: '',
            endDate: ''
        },
        search2: {
            'type':1,
            'number': '',

        }
    },
    computed: {
        getTable: function () {
            var ds = this.datas.billList,
                newBillList = [],
                billRowSpans = [];
            for (var i = 0, len = ds.length; i < len; i++) {
                var brs = 0,
                    bill = ds[i],
                    goodsList = bill.goodsList || [
                            {
                                'feeType': '',
                                'feeTypeHidden': true,
                                'payAmt': '',
                                'payAmtHidden': true,
                                'growspan': 1
                            }
                        ],
                    b = {
                        'billNo': bill.billNo,
                        'billNoHidden': false,
                        'payNo': bill.payNo,
                        'payNoHidden': false,
                        'payTime': bill.payTime,
                        'payTimeHidden': false,
                        'billStatus': bill.billStatus,
                        'billStatusHidden': false,
                        'house': bill.house,
                        'houseHidden': false,
                        'buyerName': bill.buyerName,
                        'buyerNameHidden': false
                    };
                for (var j = 0, l = goodsList.length; j < l; j++) {
                    var goods = goodsList[j],
                        tranList = goods.tranList || [
                                {
                                    'tranFlowNo': '',
                                    'payeeName': '',
                                    'payeeAmt': ''
                                }
                            ],
                        grs = tranList.length,
                        g = {
                            'feeType': goods.feeType,
                            'feeTypeHidden': false,
                            'payAmt': goods.payAmt,
                            'payAmtHidden': false,
                            'growspan': grs
                        };
                    for (var k = 0; k < grs; k++) {
                        var tran = tranList[k];
                        if (k > 0) {
                            g.feeTypeHidden = true;
                            g.payAmtHidden = true;
                            g.growspan = 1;
                            b.billNoHidden = true;
                            b.payNoHidden = true;
                            b.payTimeHidden = true;
                            b.billStatusHidden = true;
                            b.houseHidden = true;
                            b.buyerNameHidden = true;
                            b.rowspan = 1;
                        }
                        newBillList.push($.extend({}, b, g, tran));
                    }
                    brs += grs;
                }

                billRowSpans[i] = brs;
            }

            for (var i = 0, len = newBillList.length; i < len; i++) {
                var nBill = newBillList[i];
                if (!nBill.hasOwnProperty('rowspan')) {
                    nBill.rowspan = billRowSpans.pop();
                }
            }
            console.log(newBillList);
            for(var i in newBillList){
                var item = newBillList[i];

                if(item.billStatus == '0'){//0：创建， 1：成功， 2：失败， 3：取消， 4：关闭
                    item.billStatus = '创建';
                }else if(item.billStatus == '1'){
                    item.billStatus = '成功';
                }else if(item.billStatus == '2'){
                    item.billStatus = '失败';
                }else if(item.billStatus == '3'){
                    item.billStatus = '取消';
                }else {
                    item.billStatus = '关闭';
                }

                if(item.feeType == '0'){//房租、押金、水费、电费、服务费
                    item.feeType = '房租';
                }else if(item.feeType == '1'){
                    item.feeType = '押金';
                }else if(item.feeType == '2'){
                    item.feeType = '水费';
                }else if(item.feeType == '3'){
                    item.feeType = '电费';
                }else {
                    item.feeType = '服务费';
                }
            }
            return newBillList;
        },
        getDetail: function () {
            if(!this.detailShow) {
                return [];
            }else {
                var ds = [this.orderDetail],
                    newBillList = [],
                    billRowSpans = [];
                for (var i = 0, len = ds.length; i < len; i++) {
                    var brs = 0,
                        bill = ds[i],
                        goodsList = bill.goodsList || [
                                {
                                    'feeType': '',
                                    'feeTypeHidden': true,
                                    'payAmt': '',
                                    'payAmtHidden': true,
                                    'growspan': 1
                                }
                            ],
                        b = {
                            'billNo': bill.billNo,
                            'billNoHidden': false,
                            'payNo': bill.payNo,
                            'payNoHidden': false,
                            'payTime': bill.payTime,
                            'payTimeHidden': false,
                            'billStatus': bill.billStatus,
                            'billStatusHidden': false,
                            'house': bill.house,
                            'houseHidden': false,
                            'buyerName': bill.buyerName,
                            'buyerNameHidden': false
                        };
                    for (var j = 0, l = goodsList.length; j < l; j++) {
                        var goods = goodsList[j],
                            tranList = goods.tranList || [
                                    {
                                        'tranFlowNo': '',
                                        'payeeName': '',
                                        'payeeAmt': ''
                                    }
                                ],
                            grs = tranList.length,
                            g = {
                                'feeType': goods.feeType,
                                'feeTypeHidden': false,
                                'payAmt': goods.payAmt,
                                'payAmtHidden': false,
                                'growspan': grs
                            };
                        for (var k = 0; k < grs; k++) {
                            var tran = tranList[k];
                            if (k > 0) {
                                g.feeTypeHidden = true;
                                g.payAmtHidden = true;
                                g.growspan = 1;
                                b.billNoHidden = true;
                                b.payNoHidden = true;
                                b.payTimeHidden = true;
                                b.billStatusHidden = true;
                                b.houseHidden = true;
                                b.buyerNameHidden = true;
                                b.rowspan = 1;
                            }
                            newBillList.push($.extend({}, b, g, tran));
                        }
                        brs += grs;
                    }

                    billRowSpans[i] = brs;
                }

                for (var i = 0, len = newBillList.length; i < len; i++) {
                    var nBill = newBillList[i];
                    if (!nBill.hasOwnProperty('rowspan')) {
                        nBill.rowspan = billRowSpans.pop();
                    }
                }
                console.log("orderDetail:" + newBillList);
                for (var i in newBillList) {
                    var item = newBillList[i];

                    if (item.billStatus == '0') {//0：创建， 1：成功， 2：失败， 3：取消， 4：关闭
                        item.billStatus = '创建';
                    } else if (item.billStatus == '1') {
                        item.billStatus = '成功';
                    } else if (item.billStatus == '2') {
                        item.billStatus = '失败';
                    } else if (item.billStatus == '3') {
                        item.billStatus = '取消';
                    } else {
                        item.billStatus = '关闭';
                    }

                    if (item.feeType == '0') {//房租、押金、水费、电费、服务费
                        item.feeType = '房租';
                    } else if (item.feeType == '1') {
                        item.feeType = '押金';
                    } else if (item.feeType == '2') {
                        item.feeType = '水费';
                    } else if (item.feeType == '3') {
                        item.feeType = '电费';
                    } else {
                        item.feeType = '服务费';
                    }
                }
                return newBillList;
            }
        }
    },
    methods: {
        getOrderDetailList: function () {
            var _this = this;
            _this.setAjaxOptions('/getOrderDetailList', _this.search, function (result, status, jqXHR) {
                if (result.retCode == 0) {
                    _this.datas = result.data;
                    Vue.useLayerPage('laypage', _this.datas.paginator, _this.search);
                } else {
                    console.log(result.errMsg);
                }
            });
            _this.doAjax();
        },
        getOrderDetail: function () {
            var _this = this;
            _this.setAjaxOptions('/getOrderDetail', _this.search2, function (result, status, jqXHR) {
                if (result.retCode == 0) {
                    _this.orderDetail = result.data;
                    _this.detailShow = true;
                } else {
                    console.log(result.errMsg);
                }
            });
            _this.doAjax();
        },
        showBindAccount: function () {
            if (!this.isShowBindAccount) {
                this.isShowBindAccount = !this.isShowBindAccount;
            }

            Vue.useLayerForm();
        },
        showVerification: function () {
            if (!this.isShowVerification) {
                this.isShowVerification = !this.isShowVerification;
            }

            Vue.useLayerForm();
        }
    },
    mixins: [AjaxMixin, StatusMixin, UserMixin],
    mounted: function () {
        Vue.useLayerElement();
        this.search.startDate = moment().subtract(6, 'days').format('YYYY-MM-DD'); // 6天前
        this.search.endDate = moment().format('YYYY-MM-DD'); // 当前天
        Vue.useLayerDate(['startDate', 'endDate'], this.search);
        this.getOrderDetailList();
    },
    watch: {
        'search': {
            handler: function (val, oldVal) {
                this.getOrderDetailList();
            },
            deep: true
        },
        'search2': {
            handler: function (val, oldVal) {
                this.getOrderDetail();
            },
            deep: true
        }
    }
});